*******************************************************

/*  Fishing in troubled water: The Impact of US-China Trade War on Vietnam
 Pham Phuong Ngoc and Dainn Wie
 
 This is do file create all of the tables and figures for the final submission.

Inputs:

1. master.dta 

*/

/*Packges to Install
ssc install gtools
ssc install winsor2
ssc install reghdfe
ssc install ftools
ssc install outreg2
ssc install egenmore
*/

clear all
capture log close
set more off

cd "C:\Replication"

********************************************************************************
*** PART 1: Tables in Manuscript
********************************************************************************

*** Table 1: The 2018-2019 Trade War

use "Data\trade_tariff.dta",clear

*Solar/Washing Machine Feb7th, 2018

egen wave=group(m_effective_date)

gen d_tariff2017=d_tariff if year==2017

gen d_tariff_implemented=d_tariff*100 if mdate==m_effective_mdate2

gen m_val2017=m_val if year==2017
gen m_val2018=m_val if year==2018
gen m_val2019=m_val if year==2019

label var d_tariff2017 "tariff difference in 2017"
label var d_tariff_implemented "tariff difference in the month of implementation (nearest full month)"

collapse (mean) d_tariff2017 d_tariff_implemented (sum) m_val2017 m_val2018 m_val2019 (first) m_effective_date, by(wave)

drop if wave==.
order wave m_effective_date d_tariff2017 d_tariff_implemented m_val2017 m_val2018 m_val2019
save "Tables\Table1.dta", replace

browse

*The number of produt affected by each wave 

use "Data\trade_tariff.dta",clear
egen wave=group(m_effective_date)
keep if year==2018
by hs10, sort: gen order=_n if year==2018
keep if order==1

collapse (first) m_effective_date (count) hs10, by(wave)
drop if wave==.

merge 1:1 m_effective_date using "Tables\Table1.dta"
drop _merge
order wave m_effective_date hs10 d_tariff2017 d_tariff_implemented m_val2017 m_val2018 m_val2019
label define wave 1 "Solar panels & Washing machines" 2 "Aluminum, Iron, and Steel" 3 "China 1" 4 "China 2" 5 "China 3" 6 "cHINA 4"
label value wave wave 
save "Tables\Table1.dta", replace

browse

*The number of produt affected by each wave 

use "Data\trade_tariff.dta",clear
egen wave=group(m_effective_date)
keep if year==2018
by hs10, sort: gen order=_n if year==2018
keep if order==1

collapse (first) m_effective_date (count) hs10, by(wave)

browse


***Table 2. The Impact of US-China Trade War on Export from China to Vietnam 2017-2019

use "Data\cn_vn_final.dta", clear

* I drop the observation if any of HS8 codes within this category were under product-specific tariffs
drop if m_hit_max==1
keep if cn_vn==1

*2: Generate necessary Variables

gen lexp=log(exportCV)
egen ym=group(year month)

*3: Table 2 Estimation: 
*A: Products belong to both categories

sum T_share if year==2019 & vn_us==1
sum dwtar if year==2019 & vn_us==1
sum dwtar2 if year==2019 & vn_us==1

sum T_share if year==2019 
sum dwtar if year==2019 
sum dwtar2 if year==2019 


reghdfe lexp T_share vn_tariff if vn_us==1, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label replace ctitle(1) keep (T_share) addtext("Vietnam's import tariffs against Chine products, Controlled")

reghdfe lexp dwtar vn_tariff if vn_us==1, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label append ctitle(4) keep(dwtar) addtext("Vietnam's import tariffs against Chine products, Controlled")

reghdfe lexp dwtar2 vn_tariff if vn_us==1, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label append ctitle(5) keep(dwtar2) addtext("Vietnam's import tariffs against Chine products, Controlled")

*B: All products exported from China to Vietnam
reghdfe lexp T_share vn_tariff, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label append ctitle(1) keep (T_share) addtext("Vietnam's import tariffs against Chine products, Controlled")

reghdfe lexp dwtar vn_tariff, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label append ctitle(4) keep(dwtar) addtext("Vietnam's import tariffs against Chine products, Controlled")

reghdfe lexp dwtar2 vn_tariff, absorb(hs6 ym) cluster(hs6)
outreg2 using "Tables\Table2.doc", dec(3) label append ctitle(5) keep(dwtar2) addtext("Vietnam's import tariffs against Chine products, Controlled")

*** Table 3: The US Import Tariff Difference against Chinese and Vietnamese Products

use "Data\master.dta", clear

preserve
drop if year<=2017
collapse (mean) wt_tariff deta_tariff, by(year isic3)
gen industry=1
gen target=(wt_tariff!=0)
collapse (mean) average=wt_tariff (sd) std=wt_tariff (mean) average_deta=deta_tariff (sd) std_deta=deta_tariff (sum) industry (sum) target, by(year)
label variable average "Weighted Tariff Differences"
label variable std "Standard Deviation for Weighted Tariff Differences"
label variable average_deta "Simple Average Tariff Differences"
label variable std_deta "Standard Deviation for Simple Average Tariff Differences"
label variable industry "Number of industries"
label variable target "Number of target industries"

order year industry target average std average_deta std_deta

export excel using "Tables\Table3.xlsx", replace firstrow(varlabels)
restore

*** Table 4: The impact of US-China trade war on Vietnamese Firms' exportation
* Panel A: Weighted vs Simple average tariff differences - All firms

use "Data\master.dta", clear

drop if year<=2016

by ID, sort: egen obs=count(ID)
keep if obs==3

label var wt_tariff "The weighted difference in tariff"
label var vol_tariff "Tariff difference using trade volume"
label var deta_tariff "Unweighted tariff difference"

label var exporter "Exporter"
label var DE "Domestic Exporter"
label var FE "FDI Exporter"
label var FN "FDI Non-Exporter"
label var logFDI "Log of FDI"
label var logexport "Log of Export"

reghdfe exporter wt_tariff, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\Table4A.doc", label dec(3) replace ctitle(Exporter) addtext(Sample, All, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport wt_tariff, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table4A.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, All, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe exporter deta_tariff, absorb(ID isic3 year province) cluster (isic3)
sum deta_tariff if e(sample)==1 & wt_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\Table4A.doc", label dec(3) append ctitle(Exporter) addtext(Sample, All, Trade War, Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport deta_tariff, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table4A.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, All, Trade War,Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

* Panel B: Super Micro Firms vs SME and large firms
reghdfe exporter wt_tariff if size==0, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\Table4B.doc", label dec(3) replace ctitle(Exporter) addtext(Sample, Super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport wt_tariff if size==0, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
sum logexport if e(sample)==1
outreg2 using "Tables\Table4B.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe exporter wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\Table4B.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Small, medium, and large firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
sum logexport if e(sample)==1
outreg2 using "Tables\Table4B.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Small, medium, and large firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

*** Table 5: Export Responses to the U.S.–China Trade War Across Industry Sectors
*(A) Excluding non-market public services
*(1)
reghdfe exporter wt_tariff if size>0 & isic1<7, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
local mean_wt1=r(mean)
display %9.4f `mean_wt1'
sum exporter if e(sample)==1

outreg2 using "Tables\Table5.doc", label dec(3) replace  ctitle(Exporter) addtext(Sample, Excluding non-Market/Public Service, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt1') 

*(2)
reghdfe logexport wt_tariff if size>0 & isic1<7, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding non-Market/Public Service, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt1') 

*(B) Trading Sector Manufacturing & Agriculture
*(3)
reghdfe exporter wt_tariff if size!=0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
local mean_wt2=r(mean)
display %9.4f `mean_wt2'
sum exporter if e(sample)==1

outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Trading Sector, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt2') 

*(4)
reghdfe logexport wt_tariff if size!=0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Trading Sector, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt2') 


*(C) Manufacutring Only
*(5)
reghdfe exporter wt_tariff if size!=0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum wt_tariff if e(sample)==1 & wt_tariff!=0
local mean_wt3=r(mean)
display %9.4f `mean_wt3'
sum exporter if e(sample)==1

outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Manufacturing, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt3') 

*(6)
reghdfe logexport wt_tariff if size!=0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1

outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Manufacturing, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt3') 

*** Table 6: Interactive impacts of the US - China trade war and Chinese input dependency on exports

use "Data\master.dta", clear

gen out45=.
replace out45=1 if isic3<=24
replace out45=2 if isic3>=31 & isic3<=33
replace out45=3 if isic3>=51 & isic3<=62
replace out45=4 if isic3>=71 & isic3<=89
replace out45=5 if isic3>=91 & isic3<=99

replace out45=6 if isic3>=101 & isic3<=120
replace out45=7 if isic3>=131 & isic3<=152
replace out45=8 if isic3>=161 & isic3<=162
replace out45=9 if isic3>=170 & isic3<=182
replace out45=10 if isic3>=191 & isic3<=192

replace out45=11 if isic3>=201 & isic3<=203
replace out45=12 if isic3==210
replace out45=13 if isic3>=221 & isic3<=222
replace out45=14 if isic3>=231 & isic3<=239
replace out45=15 if isic3>=241 & isic3<=243

replace out45=16 if isic3>=251 & isic3<=259
replace out45=17 if isic3>=261 & isic3<=268
replace out45=18 if isic3>=271 & isic3<=279
replace out45=19 if isic3>=281 & isic3<=282
replace out45=20 if isic3>=291 & isic3<=293

replace out45=21 if isic3>=301 & isic3<=309
replace out45=22 if isic3>=310 & isic3<=332
replace out45=23 if isic3>=351 & isic3<=354
replace out45=24 if isic3>=360 & isic3<=390
replace out45=25 if isic3>=410 & isic3<=439

replace out45=26 if isic3>=451 & isic3<=479
replace out45=27 if isic3>=491 & isic3<=493
replace out45=28 if isic3>=501 & isic3<=502
replace out45=29 if isic3>=511 & isic3<=512
replace out45=30 if isic3>=521 & isic3<=523 

replace out45=31 if isic3>=531 & isic3<=533
replace out45=32 if isic3>=551 & isic3<=563
replace out45=33 if isic3>=581 & isic3<=603
replace out45=34 if isic3>=611 & isic3<=619
replace out45=35 if isic3>=620 & isic3<=639

replace out45=36 if isic3>=641 & isic3<=663
replace out45=37 if isic3>=681 & isic3<=682
replace out45=38 if isic3>=691 & isic3<=750
replace out45=39 if isic3>=771 & isic3<=829
replace out45=40 if isic3>=841 & isic3<=843

replace out45=41 if isic3>=851 & isic3<=856
replace out45=42 if isic3>=861 & isic3<=889
replace out45=43 if isic3>=900 & isic3<=932
replace out45=44 if isic3>=941 & isic3<=969
replace out45=45 if isic3>=970 & isic3<=990

merge m:1 out45 using "Data\input45.dta", keep (match master) generate (merge11)

drop merge11

drop if year<=2016

by ID, sort: egen obs=count(ID)
keep if obs==3

label var wt_tariff "The weighted difference in tariff"
label var vol_tariff "Tariff difference using trade volume"
label var deta_tariff "Unweighted tariff difference"

label var exporter "Exporter"
label var DE "Domestic Exporter"
label var FE "FDI Exporter"
label var FN "FDI Non-Exporter"
label var logFDI "Log of FDI"
label var logexport "Log of Export"

gen tariff_depend = wt_tariff*cindex
label var tariff_depend "Interaction term between tariff difference and dependency index"

gen voltariff_depend = vol_tariff*cindex
label var voltariff_depend "Interaction term between tariff difference using trade volume and dependency index"

gen deta_depend = deta_tariff*cindex
label var deta_depend "Interaction term between unweighted tariff difference and dependency index"

xtile ctile=cindex, nq(4)

reghdfe exporter i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) replace ctitle(Exporter) addtext(Sample, Excluding super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe exporter i.ctile#c.deta_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Excluding super micro firms, Trade War, Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport i.ctile#c.deta_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding super micro firms, Trade War, Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

*** Table 7: The impacts of the US-China trade war on firm investment and fixed capital

reghdfe fdi i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum fdi if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) replace ctitle(FDI (binary)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) title("Large Firms Only")

reghdfe logFDI i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum logFDI if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(LOG(FDI)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe loginvest i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum loginvest if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(LOG(Investment)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe linv_worker i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum linv_worker if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(LOG(Investment per worker)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe log_fixed i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum log_fixed if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(LOG(Fixed capital)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logfix_worker i.ctile#c.wt_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum logfix_worker if e(sample)==1
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(LOG(Fixed capital per worker)) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

*** Table 8: Testing for Pre-Existing Trends Prior to the U.S. China Trade War
use "Data\master.dta", clear

*Attach later tariff values to pre-trade war data
gen wt18_temp=.
replace wt18_temp=wt_tariff if year==2018
by isic3, sort: egen wt_tariff_18=max(wt18_temp)

gen wt19_temp=.
replace wt19_temp=wt_tariff if year==2019
by isic3, sort: egen wt_tariff_19=max(wt19_temp)

gen deta18_temp=.
replace deta18_temp=deta_tariff if year==2018
by isic3, sort: egen deta_tariff18=max(deta18_temp)

gen deta19_temp=.
replace deta19_temp=deta_tariff if year==2019
by isic3, sort: egen deta_tariff19=max(deta19_temp)

gen fake_wt=0
replace fake_wt=wt_tariff_18 if year==2015
replace fake_wt=wt_tariff_19 if year==2017

gen fake_wt2=0
replace fake_wt2=deta_tariff18 if year==2015
replace fake_wt2=deta_tariff19 if year==2017

label var fake_wt "Future tariff differences (Weighted)"
label var fake_wt2 "Future tariff differences (Average)"

keep if year==2014 | year==2015 | year==2017 
by ID, sort: egen count=count(ID)
keep if count==3

*(A) Excluding non-market public services
*(1)
reghdfe exporter fake_wt if size>0 & isic1<7, absorb(ID isic3 year province) cluster (isic3)
sum fake_wt if e(sample)==1 & fake_wt!=0
local mean_wt1=r(mean)
display %9.4f `mean_wt1'
sum exporter if e(sample)==1

outreg2 using "Tables\Table8.doc", label dec(3) replace title(Testing for Pre-Existing Trends Prior to the U.S.–China Trade War) ctitle(Exporter) addtext(Sample, Excluding non-Market/Public Service, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt1') 

*(2)
reghdfe logexport fake_wt if size>0 & isic1<7, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table8.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding non-Market/Public Service, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt1') 

*(B) Trading Sector Manufacturing & Agriculture
*(3)
reghdfe exporter fake_wt if size!=0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
sum fake_wt if e(sample)==1 & fake_wt!=0
local mean_wt2=r(mean)
display %9.4f `mean_wt2'
sum exporter if e(sample)==1

outreg2 using "Tables\Table8.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Trading Sector, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt2') 

*(4)
reghdfe logexport fake_wt if size!=0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1
outreg2 using "Tables\Table8.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Trading Sector, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt2') 


*(C) Manufacutring Only
*(5)
reghdfe exporter fake_wt if size!=0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum fake_wt if e(sample)==1 & fake_wt!=0
local mean_wt3=r(mean)
display %9.4f `mean_wt3'
sum exporter if e(sample)==1

outreg2 using "Tables\Table8.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Manufacturing, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt3') 

*(6)
reghdfe logexport fake_wt if size!=0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum logexport if e(sample)==1

outreg2 using "Tables\Table8.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Manufacturing, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)  addstat(Mean of Outcome Var: , r(mean), Average tariff differences, `mean_wt3') 

********************************************************************************
*** PART 2: Figures in Manuscript
********************************************************************************

*** Figure 1A: Differences in tariffs imposed on Chinese and Vietnamese products
use if year>=2018 using "Data\trade_tariff.dta",clear

egen wave=group(m_effective_date)


label define wave 1 "Washer & Solar"
collapse (mean) china_tariff d_tariff d_tariff_alt, by(wave mdate)

drop if wave==.
reshape wide china_tariff d_tariff d_tariff_alt, i(mdate) j(wave)

forval i=1/6 {
	rename china_tariff`i' wave`i'
	rename d_tariff`i' dwave`i'
	rename d_tariff_alt`i' daltwave`i'
}

label var wave1 "Solar & Washer"
label var wave2 "Aluminum, Iron, and Steel"
label var wave3 "China 1"
label var wave4 "China 2"
label var wave5 "China 3"
label var wave6 "China 4"

label var dwave1 "Solar & Washer"
label var dwave2 "Aluminum, Iron, and Steel"
label var dwave3 "China 1"
label var dwave4 "China 2"
label var dwave5 "China 3"
label var dwave6 "China 4"

label var daltwave1 "Solar & Washer"
label var daltwave2 "Steel"
label var daltwave3 "China 1"
label var daltwave4 "China 2"
label var daltwave5 "China 3"
label var daltwave6 "China 4"


twoway line dwave* mdate, title("Differences in Tariffs on Imports to United States: China vs. Vietnam", size(medium))  subtitle("During the Trade War 2018-2019") lpattern(shortdash_dot shortdash solid dash longdash longdash_dot)


graph export "Figures\figure1a.pdf", replace
graph export "Figures\figure1a.emf", replace

*** Figure 1B: Differences in tariffs imposed on Chinese and Vietnamese products - Ignoring later exemptions

twoway line daltwave* mdate, title("Differences in Tariffs on Imports to United States: China vs. Vietnam", size(medium))  subtitle("Ignoring Later Exclusions During the Trade War 2018-2019") lpattern(shortdash_dot shortdash solid dash longdash longdash_dot)

graph export "Figures\figure1b.pdf", replace
graph export "Figures\figure1b.emf", replace

*** Figure 2: The impact of trade war on Vietnam's export to the US
use "Data\trade_tariff.dta",clear

*Read in the data
use if year>=2018 using "Data\trade_tariff.dta",clear

*Scale
foreach j in val q1 p pduty {
	replace m_`j' = 1000000*m_`j'
	}


*Generate outcome variables in Log

gen lval=log(m_val)
gen lq1=log(m_q1)
gen lp=log(m_p)
gen lpduty=log(m_pduty)

label var lval "Log Value in USD"
label var lq1 "Log Quantity"
label var lp "Log Unit Value in USD"
label var lpduty "Log Duty-Inclusive Unit Value in USD"

*Event Study Construction

egen wave=group(m_effective_date)
gen control=(wave==.)

*Sample Selections
drop if wave==1
drop if wave==2
drop if wave==6

*Event day
gen etemp=mdate if mdate==m_effective_mdate2
by wave, sort: egen emonth=max(etemp)
format emonth %tm
gen rel_month=mdate-emonth
keep if (control == 1) | (inrange(rel_month, -6, 8))

gen cohort = .
replace cohort = mdate if wave == 2 & rel_month == 0
replace cohort = mdate if wave == 3 & rel_month == 0
replace cohort = mdate if wave == 5 & rel_month == 0

*Relatvie Time Variables
forvalues i=2/6 {
	gen g_m`i'=(rel_month==-`i')
}

forvalues i=0/8 {
	gen g_`i'=(rel_month==`i')
}


*Sun & Abrahams Estimation

eventstudyinteract lval g_*, cohort(wave) absorb(hs10 mdate) control_cohort(control) vce(cluster hs10)

gen beta = .
gen se= .
forvalues i=1/5 {
	replace beta=100*e(b_iw)[1,`i'] if rel_month==-(`i'+1)
	scalar temp_se = sqrt(e(V_iw)[`i',`i'])
	replace se=100*temp_se if rel_month==-(`i'+1)
}

forvalues i=6/14 {
	replace beta=100*e(b_iw)[1,`i'] if rel_month==`i'-6 
	scalar temp_se = sqrt(e(V_iw)[`i',`i'])
	replace se=100*temp_se if rel_month==`i'-6 
}

replace beta=0 if rel_month==-1
replace se=0 if rel_month==-1


collapse (first) beta se, by(rel_month)
drop if beta==.

*Confidence Intervals
gen ud = beta + 1.96*se
gen ld = beta - 1.96*se

*Figure
	
twoway (con ld rel_month) (con ud rel_month) (con beta rel_month), xtitle("Months Relative to Tariff Enactment", size(med)) ytitle("Percent", size(med)) ylabel(`ylab',labsize(small) nogrid) xlabel(-6(1) 8, valuelabel) legend(off) title("US-China Trade War and Vietnam's Export to US") subtitle("Log Value in USD") yline(0, lpattern(dash) lcolor(black))
		
graph save "Figures\fig2.gph",replace 
graph export "Figures\fig2.emf", replace

********************************************************************************
*** PART 3: Tables in Online Appendix
********************************************************************************

*** Table A2: China's Export to Vietnam vs. Vietnam's Export to the United States

*Table A2 Columns (1) and (2)

use "Data\cn_vn_final.dta", clear

keep if cn_vn==1
*export(CV) is already in 1000USD
tab year
by year, sort: egen amount=sum(exportCV)
replace amount=amount/1000000
by year, sort: sum amount
label var amount "Annual China's export to VN in billions"


collapse (sum) exportCV (first) amount, by(year hs6)

drop if exportCV==0
* # of 6-digit HS codes per year
tab year
* Total amount of China's Export to Vietnam in Billion USD
tab amount if year==2017
tab amount if year==2018
tab amount if year==2019


*Table A2 Column (3) 

*Table A2, Sample A: column (3) % HS6 under China tariff

use "Data\cn_vn_final.dta", clear
keep if cn_vn==1

by year, sort: sum T_share
drop if T_share==.


*Table A2, Sample A: column (3) % HS6 under China tariff
gen hit=(T_share>0)


collapse (max) hit, by(hs6 year)
by year, sort: tab hit

*Table A2: Column (4), (5), and (6)

use "Data\cn_vn_final.dta", clear

*Column (4) 
keep if vn_us==1
collapse (sum) exportCV, by(hs6 year)

tab year

*Column (5)
by year, sort: egen amount=sum(exportCV)
replace amount=amount/1000000

* Total amount of Vietnam's export to US in Billion USD
tab amount if year==2017
tab amount if year==2018
tab amount if year==2019

*Column (6)
use "Data\cn_vn_final.dta", clear
keep if vn_us==1

by hs6 year, sort: egen cn_vn_max=max(cn_vn)

collapse (first) cn_vn_max, by(year hs6)

by year, sort: tab cn_vn_max

*** Table A3: Vietnamese Enterprise Survey Data Summary Statistics

* Open VFS dataset
use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

by ID, sort: egen obs=count(ID)
keep if obs==3

* calculate the mean and standard deviation
* All sample
preserve
collapse exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year
gen sample=1
gen statistic=1
save "Data\All_sample_mean.dta", replace
restore

preserve
collapse (sd)exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year
gen sample=1
gen statistic=2
save "Data\All_sample_sd.dta", replace
restore

* Before trade war
preserve
keep if year==2017
collapse exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year 
gen sample=2
gen statistic=1
save "Data\before_mean.dta", replace
restore

preserve
keep if year==2017
collapse (sd) exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year 
gen sample=2
gen statistic=2
save "Data\before_sd.dta", replace
restore

* After trade war
preserve
keep if year ==2018 | year==2019
collapse exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year 
gen sample=3
gen statistic=1
save "Data\after_mean.dta", replace
restore

preserve
keep if year ==2018 | year==2019
collapse (sd) exporter logexport fdi logFDI loginvest linv_worker log_fixed logfix_worker (count) nobs=year 
gen sample=3
gen statistic=2
save "Data\after_sd.dta", replace
restore

* append data together 
preserve
use "Data\All_sample_mean.dta", clear
append using "Data\All_sample_sd.dta"
append using "Data\before_mean.dta"
append using "Data\before_sd.dta"
append using "Data\after_mean.dta"
append using "Data\after_sd.dta"

order sample statistic
sort sample statistic
label define sample_lbl 1"All" 2"Before Trade War" 3"After Trade War"
label values sample sample_lbl
label define stat_lbl 1"Mean" 2 "Std.Dev"
label values statistic stat_lbl

label variable exporter "Indicator for export company"
label variable logexport "Logarithm of export volume"
label variable fdi "Indicator for FDI company"
label variable logFDI "Logarithm of FDI amount"
label variable loginvest "Logarithm of investment amount"
label variable linv_worker "Logarithm of investment per worker"
label variable log_fixed "Logarithm of fixed capital amount"
label variable logfix_worker "Logarithm of fixed capital per worker"
label variable nobs "Number of observations"

export excel using "Tables\TableA3.xlsx", replace firstrow(varlabels)
erase "Data\All_sample_mean.dta"
erase "Data\All_sample_sd.dta"
erase "Data\before_mean.dta"
erase "Data\before_sd.dta"
erase "Data\after_mean.dta"
erase "Data\after_sd.dta"
restore

*** Table A4: Replication of Table 4, Panel B Using an Alternative Weighted Tariff Measure
use "Data\master.dta", clear

drop if year<=2016

by ID, sort: egen obs=count(ID)
keep if obs==3

label var deta_tariff "Unweighted tariff difference"

label var exporter "Exporter"
label var DE "Domestic Exporter"
label var FE "FDI Exporter"
label var FN "FDI Non-Exporter"
label var logFDI "Log of FDI"
label var logexport "Log of Export"

reghdfe exporter deta_tariff if size==0, absorb(ID isic3 year province) cluster (isic3)
sum deta_tariff if e(sample)==1 & deta_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\TableA4.doc", label dec(3) replace ctitle(Exporter) addtext(Sample, Super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport deta_tariff if size==0, absorb(ID isic3 year province) cluster (isic3)
sum deta_tariff if e(sample)==1 & deta_tariff!=0
sum logexport if e(sample)==1
outreg2 using "Tables\TableA4.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Super micro firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe exporter deta_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum deta_tariff if e(sample)==1 & deta_tariff!=0
sum exporter if e(sample)==1
outreg2 using "Tables\TableA4.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Small, medium, and large firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe logexport deta_tariff if size>0, absorb(ID isic3 year province) cluster (isic3)
sum deta_tariff if e(sample)==1 & deta_tariff!=0
sum logexport if e(sample)==1
outreg2 using "Tables\TableA4.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Small, medium, and large firms, Trade War, Weighted Tariff Differences, Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

*** Table A5: The Impacts of the US-CHINA Trade War on Firm Investment and Fixed Capital in Manufacturing Sector
use "master.dta", clear

gen out45=.
replace out45=1 if isic3<=24
replace out45=2 if isic3>=31 & isic3<=33
replace out45=3 if isic3>=51 & isic3<=62
replace out45=4 if isic3>=71 & isic3<=89
replace out45=5 if isic3>=91 & isic3<=99

replace out45=6 if isic3>=101 & isic3<=120
replace out45=7 if isic3>=131 & isic3<=152
replace out45=8 if isic3>=161 & isic3<=162
replace out45=9 if isic3>=170 & isic3<=182
replace out45=10 if isic3>=191 & isic3<=192

replace out45=11 if isic3>=201 & isic3<=203
replace out45=12 if isic3==210
replace out45=13 if isic3>=221 & isic3<=222
replace out45=14 if isic3>=231 & isic3<=239
replace out45=15 if isic3>=241 & isic3<=243

replace out45=16 if isic3>=251 & isic3<=259
replace out45=17 if isic3>=261 & isic3<=268
replace out45=18 if isic3>=271 & isic3<=279
replace out45=19 if isic3>=281 & isic3<=282
replace out45=20 if isic3>=291 & isic3<=293

replace out45=21 if isic3>=301 & isic3<=309
replace out45=22 if isic3>=310 & isic3<=332
replace out45=23 if isic3>=351 & isic3<=354
replace out45=24 if isic3>=360 & isic3<=390
replace out45=25 if isic3>=410 & isic3<=439

replace out45=26 if isic3>=451 & isic3<=479
replace out45=27 if isic3>=491 & isic3<=493
replace out45=28 if isic3>=501 & isic3<=502
replace out45=29 if isic3>=511 & isic3<=512
replace out45=30 if isic3>=521 & isic3<=523 

replace out45=31 if isic3>=531 & isic3<=533
replace out45=32 if isic3>=551 & isic3<=563
replace out45=33 if isic3>=581 & isic3<=603
replace out45=34 if isic3>=611 & isic3<=619
replace out45=35 if isic3>=620 & isic3<=639

replace out45=36 if isic3>=641 & isic3<=663
replace out45=37 if isic3>=681 & isic3<=682
replace out45=38 if isic3>=691 & isic3<=750
replace out45=39 if isic3>=771 & isic3<=829
replace out45=40 if isic3>=841 & isic3<=843

replace out45=41 if isic3>=851 & isic3<=856
replace out45=42 if isic3>=861 & isic3<=889
replace out45=43 if isic3>=900 & isic3<=932
replace out45=44 if isic3>=941 & isic3<=969
replace out45=45 if isic3>=970 & isic3<=990

merge m:1 out45 using input45.dta, keep (match master) generate (merge11)

drop merge11

drop if year==2014 | year==2016 | year==2015

label var wt_tariff "The weighted difference in tariff"
label var vol_tariff "Tariff difference using trade volume"
label var deta_tariff "Unweighted tariff difference"

label var exporter "Exporter"
label var DE "Domestic Exporter"
label var FE "FDI Exporter"
label var FN "FDI Non-Exporter"
label var logFDI "Log of FDI"
label var logexport "Log of Export"

gen tariff_depend=wt_tariff*cindex
label var tariff_depend "Interaction term between tariff difference and dependency index"

gen voltariff_depend = vol_tariff*cindex
label var voltariff_depend "Interaction term between tariff difference using trade volume and dependency index"

gen deta_depend = deta_tariff*cindex
label var deta_depend "Interaction term between unweighted tariff difference and dependency index"

xtile ctile=cindex, nq(4)
gen inv_worker=rinvest/cur_employees
gen linv_worker=log(inv_worker+1)
gen lnexport=log(rexport+1)

gen cindex2=.
replace cindex2=cindex if (manu==1 | agri==1)
xtile ctile2=cindex2, nq(4)

gen cindex3=.
replace cindex3=cindex if manu==1
xtile ctile3=cindex3, nq(4)

by ID, sort: egen obs=count(ID)
keep if obs==3

*(1)
reghdfe fdi i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum fdi if e(sample)==1

outreg2 using "Tables\TableA5.doc", label dec(3) replace ctitle(FDI) addstat(Mean of Outcome Var: , r(mean), STD: , [r(sd)])  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) title("Table A4. The Impacts of the US-CHINA Trade War on Firm Investment and Fixed Capital in Manufacturing Sector") 

*(2)
reghdfe logFDI i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum logFDI if e(sample)==1

outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Log(FDI)) addstat(Mean of Outcome Var: , r(mean), STD: , r(sd))  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) 

*(3)
reghdfe loginvest i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum loginvest if e(sample)==1
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Log(Investment)) addstat(Mean of Outcome Var: , r(mean), STD: , r(sd))  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) 

*(4)

reghdfe linv_worker i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum linv_worker if e(sample)==1
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Log(Investment per worker)) addstat(Mean of Outcome Var: , r(mean), STD: , r(sd))  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) 

*(5)
reghdfe log_fixed i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum log_fixed if e(sample)==1
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Log(Fixed capital)) addstat(Mean of Outcome Var: , r(mean), STD: , r(sd))  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) 

*(6)
reghdfe logfix_worker i.ctile3#c.wt_tariff if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
sum logfix_worker if e(sample)==1
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Log(Fixed capital per worker)) addstat(Mean of Outcome Var: , r(mean), STD: , r(sd))  addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes, Sample, Excluding Super Micro Firms) 


*** Table A6: Replication of Table 6 Using Pre-Trade War Data
use "Data\master.dta", clear

*Attach later tariff values to pre-trade war data
gen wt18_temp=.
replace wt18_temp=wt_tariff if year==2018
by isic3, sort: egen wt_tariff_18=max(wt18_temp)

gen wt19_temp=.
replace wt19_temp=wt_tariff if year==2019
by isic3, sort: egen wt_tariff_19=max(wt19_temp)

gen deta18_temp=.
replace deta18_temp=deta_tariff if year==2018
by isic3, sort: egen deta_tariff18=max(deta18_temp)

gen deta19_temp=.
replace deta19_temp=deta_tariff if year==2019
by isic3, sort: egen deta_tariff19=max(deta19_temp)

gen fake_wt=0
replace fake_wt=wt_tariff_18 if year==2015
replace fake_wt=wt_tariff_19 if year==2017

gen fake_wt2=0
replace fake_wt2=deta_tariff18 if year==2015
replace fake_wt2=deta_tariff19 if year==2017

label var fake_wt "Future tariff differences (Weighted)"
label var fake_wt2 "Future tariff differences (Average)"

keep if year==2014 | year==2015 | year==2017 
by ID, sort: egen count=count(ID)
keep if count==3

*(1)
reghdfe exporter i.ctile#c.fake_wt if size>0,  absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) replace ctitle(Exporter) addtext(Sample, Excluding super micro firms, Sector, All, Trade War, Weighted Tariff Differences) 
*(2)
reghdfe logexport i.ctile#c.fake_wt if size>0, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding super micro firms,  Sector, All, Trade War, Weighted Tariff Differences) 


*(3)
*Trading Sector Only
reghdfe exporter i.ctile2#c.fake_wt if size>0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Excluding super micro firms, Sector, Trading, Trade War, Weighted Tariff Differences) 

*(4)
reghdfe logexport i.ctile2#c.fake_wt if size>0 & (manu==1 | agri==1), absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding super micro firms,  Sector, Trading, Trade War, Weighted Tariff Differences) 


*Manufacturing Only Weighted Tariff
*(5)
reghdfe exporter i.ctile3#c.fake_wt if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Exporter) addtext(Sample, Excluding super micro firms, Sector, Manufacturing, Trade War, Weighted Tariff Differences) 

*(6)
reghdfe logexport i.ctile3#c.fake_wt if size>0 & manu==1, absorb(ID isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Log(Export)) addtext(Sample, Excluding super micro firms,Sector, Manufacturing, Trade War, Weighted Tariff Differences)
*/
